package com.genius.smsbase.Domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * @ClassName BaseEntity
 * @Description BaseEntity 包括实体创建时间，实体修改时间，id
 * @author：
 */
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
@Access(value = AccessType.FIELD)
public class BaseEntity<PK extends Serializable> extends BaseDomain {

    /**
     *
     */
    private static final long serialVersionUID = -6163675075289529459L;
    /**
     * 实体创建时间
     */
    @CreatedDate
    @Column(name = "createTime",updatable = false)
    protected Date createTime;

    /**
     * 实体修改时间
     */
    @LastModifiedDate
    @Column(name = "updateTime")
    protected Date updateTime;



    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected PK id;


    @Override
    public boolean equals(Object obj) {
        if (null != obj) {
            if (obj instanceof BaseEntity) {
                @SuppressWarnings("unchecked")
                BaseEntity<PK> domain =  (BaseEntity<PK>) obj;
                if (this.id == domain.id) {
                    return true;
                }
            }
        }
        return false;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public PK getId() {
        return id;
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this.id);
    }



    public void setId(PK id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

}
